#include <print_error.h>

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
// link with -lboost_filesystem-gcc

#include <string>
#include <algorithm>
#include <fstream>
#include <stdexcept>
using std::ifstream;
using std::istream;
using std::string;

#include "open_fstream.h"


/*
++dots;
if (dots.value() % (DOTS_SIZE * 10) == 0)
{
boost::intmax_t file_size_so_far = ifs.tellg();
unsigned dots_needed = unsigned(dots.value() * 1.0 *
				file_size / file_size_so_far);
dots_needed += DOTS_SIZE;
dots.set_limit(dots_needed);
}
*/

//________________________________________________________________________________________

//		open_existing_file

//________________________________________________________________________________________
istream::pos_type open_fstream(ifstream& ifs, const string& file_name)
{
	namespace fs = boost::filesystem;
	if (!fs::exists(file_name))
		throw std::runtime_error("\"" + file_name + "\" does not exist.");
	if (fs::is_directory(file_name))
		throw std::runtime_error("\"" + file_name + "\" is a directory not a file.");
	boost::intmax_t file_size = fs::file_size( file_name);
	ifs.open(file_name.c_str());
	if (!ifs)
		throw std::runtime_error("Could not open \"" + file_name + "\"");
	return file_size;
}

